package controller;

import WebSocket.WebsocketEndPoint;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import modal.RegisterUser;
import modal.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import service.LoginService;
import service.PersonManageService;
import service.RegisterService;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.websocket.server.PathParam;
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;


@Controller
public class Login {

    @Autowired
    LoginService loginService;

    @Autowired
    RegisterService registerService;

    @Autowired
    PersonManageService personManageService;
    //用户登录，并且把用户信息存到session中
    @RequestMapping(value = "/loginvalidate.do",produces = "application/json; charset=utf-8" )
    @ResponseBody
    public String validate( User user, HttpSession httpSession, HttpServletResponse response, HttpServletRequest request)
    {
        response.setHeader("Access-Control-Allow-Origin","*");

        JSONObject jsonObject = new JSONObject();
//        测试输出结果
        System.out.println(user.getUsername()+"  "+user.getPassword());
        System.out.println(loginService.loginValidate(user));
        String result=loginService.loginValidate(user);
        if(result.equals("success"))
        {
            User newUser = loginService.getOneUser(user.getUsername());
            httpSession.setAttribute("user",newUser);
            jsonObject.put("user",JSON.toJSONString(newUser, SerializerFeature.WriteMapNullValue));
        }
        jsonObject.put("result",result);
        return  jsonObject.toJSONString();
    }
    /**
     * 注册用户
    * @param registerUser 注册用户信息
     */
    @RequestMapping(value = "/register.do")
    @ResponseBody
    public String Register(HttpServletResponse response, RegisterUser registerUser)
    {
        response.setHeader("Access-Control-Allow-Origin","*");
        User user=new User();
        user.setUsername(registerUser.getUsername());
        user.setPassword(registerUser.getPassword());
        JSONObject jsonObject = new JSONObject();
        if(loginService.getOneUser(user.getUsername())!=null)
        {
            jsonObject.put("result","error");
        }
        else
        {
            String result=registerService.registerUser(user);
            System.out.println(user.getUsername()+"  "+user.getPassword());
            jsonObject.put("result",result);
        }
        return  jsonObject.toJSONString();
    }



    //判断session是否为空
    @RequestMapping(value = "/getSession.do")
    @ResponseBody
    public String GetSession(HttpSession session,HttpServletResponse response)
    {
        response.setHeader("Access-Control-Allow-Origin","*");
        response.setHeader("Cache-Control","no-cache");
        JSONObject jsonObject = new JSONObject();
        if(session.getAttribute("user")!=null)
        {
            User user= (User) session.getAttribute("user");
            jsonObject.put("user",personManageService.getOneUser(user.getUsername()));
            jsonObject.put("result","success");
        }
        else {
            jsonObject.put("result","error");
        }
        return jsonObject.toJSONString();
    }

    //注销session
    @RequestMapping(value = "/deleteSession.do")
    @ResponseBody
    public void deleteSession(HttpSession session)
    {
        session.invalidate();
    }

    @RequestMapping(value = "/v/{roomid}")
    public void redirect(@PathVariable(value = "roomid") String roomid,HttpServletResponse response) throws IOException {
        System.out.println(roomid);
        response.sendRedirect("http://10.99.10.15:8080/static/project/view/room/live.html?roomid="+roomid);
    }
}
